/**
 * <strong>Title : ShutDownCause.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月4日 下午4:55:40<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.ShutDownCause;
import cn.b.sky.mes.em.model.condition.ShutDownCauseCondition;
import cn.b.sky.mes.em.service.ShutDownCauseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : ShutDownCauseController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>停机原因代码控制器<br> 
 * <strong>Create on : 2015年11月4日 下午4:55:40<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/shutDownCause/*")
public class ShutDownCauseController extends SecurityController {

  @Resource
  ShutDownCauseService shutDownCauseService;

  @RequestMapping("shutDownCause_list")
  public String selectShutDownCauseList(String navTabId, Model model,
      @ModelAttribute("condition") ShutDownCauseCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<ShutDownCause> list = shutDownCauseService.selectShutDownCauseListByCondition(condition);
    int count = shutDownCauseService.selectShutDownCauseCount(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/shutDownCause/shutDownCause_list";
  }

  @RequestMapping("addShutDownCause")
  public String addShutDownCause(String navTabId, Model model, String id) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");
    return "em/shutDownCause/shutDownCause_add";
  }

  @RequestMapping("modifyShutDownCause")
  public String modifyShutDownCause(String navTabId, Model model, String id) {
    ShutDownCause cause = shutDownCauseService.getShutDownCause(id, getSiteCode());
    model.addAttribute("shutDownCause", cause);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "modify");
    return "em/shutDownCause/shutDownCause_add";
  }

  @RequestMapping("validateShutDownCauseCode")
  @ResponseBody
  public boolean validateShutDownCauseCode(String shutDownCauseCode) {
    if (StringUtils.isEmpty(shutDownCauseCode)) {
      return true;
    }
    ShutDownCause shutDownCause = shutDownCauseService.getShutDownCause(shutDownCauseCode, getSiteCode());
    return shutDownCause == null;
  }

  @RequestMapping("viewShutDownCause")
  public String viewShutDownCause(String navTabId, Model model, String id) {
    ShutDownCause cause = shutDownCauseService.getShutDownCause(id, getSiteCode());
    model.addAttribute("shutDownCause", cause);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "read");
    return "em/shutDownCause/shutDownCause_add";
  }

  @RequestMapping("saveShutDownCause")
  @ResponseBody
  public DWZResult saveShutDownCause(String navTabId, Model model, String id, ShutDownCause shutDownCause) {
    if (shutDownCause == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    if (shutDownCause.getLockFlag() == null) {
      shutDownCause.setLockFlag(0);
    }
    setCreateInfo(shutDownCause);
    shutDownCause.setSiteCode(getSiteCode());
    if (StringUtils.isBlank(id)) {
      shutDownCauseService.saveShutDownCause(shutDownCause);
    } else {
      shutDownCauseService.updateShutDownCause(shutDownCause);

    }
    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, null, null);

  }

  @RequestMapping("deleteShutDownCause")
  @ResponseBody
  public DWZResult deleteShutDownCause(HttpServletRequest request, String navTabId) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] shutDownCauseCodes = ids.split(",");
    int len = shutDownCauseCodes.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = shutDownCauseService.deleteShutDownCause(shutDownCauseCodes[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("checkShutDownCauseCode")
  @ResponseBody
  public boolean checkShutDownCauseCode(String shutDownCauseCode) {
    if (!StringUtils.isEmpty(shutDownCauseCode)) {
      ShutDownCause cause = shutDownCauseService.getShutDownCause(shutDownCauseCode, getSiteCode());
      return cause != null && cause.getLockFlag() != 1;
    } else {
      return true;
    }
  }

  @RequestMapping("shutDownCauseLookup")
  public String shutDownCauseLookup(String navTabId, Model model,
      @ModelAttribute("condition") ShutDownCauseCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<ShutDownCause> list = shutDownCauseService.selectShutDownCauseListByCondition(condition);
    int count = shutDownCauseService.selectShutDownCauseCount(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/shutDownCause/shutDownCause_lookup";
  }

  @RequestMapping("shutDownCauseSuggest")
  @ResponseBody
  public List<ShutDownCause> shutDownCauseSuggest(String navTabId, Model model,
      @ModelAttribute("condition") ShutDownCauseCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<ShutDownCause> list = shutDownCauseService.selectShutDownCauseForSuggest(condition);
    condition.setData(list);
    return list;
  }
}
